<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/back/css/base.css" rel="stylesheet" type="text/css" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>

<style>
.noselect{
	background-color: rgba(99, 230, 99, 0.8);
}
</style>	
</head>
<body>
	[#if contents ?? && maxlength!=0 ]
	<div class="panel panel-info panel-default" style="overflow: auto;" >
		<div class="panel-heading"  style="position: fixed;height: 70px;width: 99%;" >
			<table>
				<tr>	
					名称: ${enquiry.title}
				</tr>
				<tr>
					<td class="w80 ">
						<button type="button" class="btn  btn-success"  onclick="submodel()" >确定</button>
					</td>
			
					<td class="w80 ">
						<button type="button" class="btn  btn-info"  onclick="shownocontent()" >查看未匹配行</button>
					</td>
					<td>
						<button  id="mulityset"  type="button" class="btn  btn-primary " style="display: none;"   onclick="mulityset()" >批量设置系统</button>
					</td>
					<td>
						[#if titlePerfect]&nbsp;&nbsp;<b id="icon_140"  class="glyphicon glyphicon-thumbs-up"></b>[/#if]
						[#if contentPerfect ]&nbsp;&nbsp;<b id="icon_140"  class="glyphicon glyphicon-thumbs-up">&nbsp;&nbsp;&nbsp;&nbsp;</b>[/#if]
					</td>
				
				</tr>
				
			</table>
		</div>
	<form id="itemform"  action="/purchase/o_saveitem2.do" method="post"   style="padding-top: 70px;"  >
		<input type="hidden" value="${enquiry.id}" name="enquiryid"  id="enquiryid" />
		<div >
		<table class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th>#</th>
					<th>系统名称</th>
					[#assign  index = maxlength /]
					
					[#list 1..index  as b]
					<th class="cell_${b}">
						<select class="typename" name="typename" attnum=${b} >
						
							[#if titleindex?? && (titleindex?size > b-1) ]
									[#assign a = titleindex[b-1] ]
							[#else]									
									[#assign a = 0 ]
							[/#if ]
							
							<option  value="0"  >请选择</option>
							<option  [#if a==1] selected="selected" [/#if] value="1"  >设备名称</option>
							<option  [#if a==2] selected="selected" [/#if] value="2" >品牌</option>
							<option  [#if a==3] selected="selected" [/#if] value="3" >型号</option>
							<option  [#if a==4] selected="selected" [/#if] value="4" >单位</option>
							<option  [#if a==5] selected="selected" [/#if] value="5" >数量</option>
							<option  [#if a==6] selected="selected" [/#if] value="6" >技术参数</option>
							<option  [#if a==7] selected="selected" [/#if] value="7" >单价</option>
						</select>
						
					</th>
					[/#list]
				</tr>
				<tr>
					<td>
					<span>
						全<input type="checkbox" class="checkedall"  >
					</span>
					</td>
					<td>
					<span style="font-size: 10px;color: #918C8C;" >原始名称 ：</span>
					</td>
					[#list 1..index  as a]
					<td  class="cell_${a}">
						<button  onclick="delcell(${a})" class="btn btn-xs btn-danger" >删除该列</button>
						[#--原始列名--]
						[#if titles?? && (titles?size > a-1) ]
							<span style="font-size: 10px;color: #918C8C;" >${titles[a-1]}</span>
							<input  type="hidden"   name="oldtitle"  value="${titles[a-1]}"    />
						[/#if ]
					</td>	
					[/#list]
				</tr>
			</thead>
			[#assign  index = 0 /]
				[#list contents as cc ]
					<tr>
						<td>
							<label >
							<span>${index+1}</span>
							<span>
								<input type="checkbox"  value="${index}"  class="checksystem"    />
							</span>
							</label>
							<input type="hidden"  name=".enquiryId" value="${enquiry.id!}"  />
						</td>

						[#list cc as c]
							[#if c_index==0]
								<td>
								<input id="system_${index}" type="button"   attshebei=[#if  md5secret?? && md5secret?size!=0 && shebeiindex!=-1 ] "${md5secret[index]}" [#else]""[/#if]    value="[#if cc[0]=="" ]选择系统[#elseif cc[0]?index_of(",")!=-1 ]有多个匹配[#else]${categorysMap[cc[0]].name!}[/#if]"  class="w120 btn [#if cc[0]=="" ]btn-default[#elseif cc[0]?index_of(",")!=-1  ]btn-danger[#else]btn-success[/#if]"   onclick="chooseService(this,${index+1})"   />
								<input class="fcateid"  type="hidden" name=".categoryPId"  value="[#if cc[0]!="" ]${cc[0]}[/#if]"  />
								<input  type="hidden" name=".categorySId"    />
								[#--无法识别的系统，需要保存到数据库--]
								<input type="hidden" name=".needrepository"   value=[#if cc[0]==""]1[#else]0[/#if]  >
								</td>
							[#else]
								<td class="cell_${c_index}"><input   name=""    class="input_${c_index}" type="text"  value="${c!?html}" > </td>
							[/#if]
						[/#list]
						[#if cc?size-1 < maxlength]
							[#--补充空白列--]
							[#list 0..(maxlength - cc?size) as c]
								<td class="cell_${c_index + cc?size}"><input   name=""    class="input_${c_index+ cc?size}" type="text"  value="" > </td>
							[/#list]
						[/#if]
						
					</tr>
					[#assign  index = index+1 /]
				[/#list]

			
		</table>
		</div>
		</form>
	</div>
	<div style="display: none;" >
		<div id="nocontent" >
		<div style="overflow: scroll;max-height: 500px;min-width:300px;">
			[#if nocontents?? ]
			<table >
			[#list nocontents  as nocontent]
				<tr>
					[#list nocontent  as c]
						[#if c!=""]
							<td  style="border-bottom: 1px solid black;" >&nbsp;${c!}&nbsp;</td> 
						[/#if]	
					[/#list]
				</tr>
			[/#list]	
			</table>
			[#else]
				没有
			[/#if]
		</div>
		</div>
		
	</div>
	

	<script type="text/javascript">
		//设备名称，品牌 ，型号  ,单位 ,数量,技术参数,单价
		var selectnames =["productName","industry","model","unit","count","descr","unitprice"];
	    //切换选项，确保不重复就可
		$(".typename").on("change",function(){
			
			var  thisval = $(this).val();
			
			if(thisval!=0){//将相同的选项设置为请选择
				var hadvnum =[];
				hadvnum.push(thisval);
				
				$(".typename").each(function(i){
					var otherv = $(this).val();
					if(otherv == thisval ){
						$(this).val(0);
					}else{
						hadvnum.push(otherv);
					}
				});
				
				//没有被选择的加颜色
 				var nums = hadvnum.join("");
				$(".noselect").removeClass("noselect");
				for(var i=1;i<7;i++){
					if(nums.indexOf(i)==-1){
						$(".typename").find("option[value='"+i+"']").addClass("noselect");
					}
				} 
				
			}
			
			$(this).val(thisval);
		});
		//准备提交
		function submodel(){
	    	
			var success = true;
			var msg ="";
			$(".typename").each(function(){
				var thisval = $(this).val();
				if(thisval==0){
					success = false;
					msg="失败：请确定每一列的名称";
					return false;
				}
				
			});
			
			if(!success){
				art.dialog.tips(msg);
				return ;
			}else{
				//确定每列的name
				$(".typename").each(function(i){
					var thisval = $(this).val();
					var attnum = $(this).attr("attnum");
					//拿到name值
					var namev = selectnames[thisval-1];
					//设置name值
					$(".input_"+attnum).each(function(){
                        $(this).attr("name","."+namev);
					})

				});


				$(".fcateid").each(function(){
					var v = $(this).val();
					if(v=="" || v.indexOf(",")!=-1){
						success=false;
						return false;
					}
				});
				if(!success){
					art.dialog.tips("必须选择系统！");
					return ;
				}


				//验证设备名称
				$("#itemform").find("input[name$='productName']").each(function(){
					var productname = $(this).val();
					if(productname==""){
						success = false;
						return ;
					}
				});

				if(!success){
					art.dialog.tips("设备名称不能有空！");
					return ;
				}

                //验证数值 ,数量必须全为数字
                $("#itemform").find("input[name$='quantity']").each(function(){
                    var num = $(this).val();
                    if(num=="" || isNaN(num) || parseFloat(num)<0 ){
                        success = false;
                        return ;
                    }
                });

                if(!success){
                    art.dialog.tips("数量对应的列必须全为数字且不能小于0!");
                    return ;
                }

                //验证价格,价格全部为数字
                $("#itemform").find("input[name$='unitprice']").each(function(){
                    var pirce = $(this).val();
                    if(pirce=="" || isNaN(pirce) || parseFloat(pirce) <0 ){
                        success = false;
                        return ;
                    }
                });

                if(!success){
                    art.dialog.tips("单价对应的列必须全为数字且不能小于0!");
                    return ;
                }



                var items =[];
                var typenames =[];
                var oldtitles=[];
                $("#itemTable").find("tr").each(function () {
                    var obj={};
                    $(this).find("input[name]").each(function () {
                        var names = $(this).attr("name");
                        var inputval = $(this).val();
                        if(names.indexOf(".")!=-1){
                            var k = names.split(".")[1];
                            obj[k]=inputval;
                        }else{
                            if(names=="oldtitle"){
                                oldtitles.push(inputval);
                            }
                        }
                    });
                    if(!jQuery.isEmptyObject(obj)){
                        items.push(obj);
                    }
                });

                $("select[name='typename']").each(function(){
                    typenames.push($(this).val());
                });

                var obj={item:items,typenames:typenames,oldtitles:oldtitles,enquiryid:$("#enquiryid").val(),type:2};



                //设置后提交
  				art.dialog.post({
					url:"/enquiry/o_saveitem.do",
                    data:JSON.stringify(obj),
                    contentType:"application/json",
					success:function(data){
						if(data.success){
							//refresh();
							//更新原
							art.dialog.alert("操作成功",function(){
								top.closeTempTab(true);
							})
						}else{
							art.dialog.tips(data.msg);
						}
					}
				});

			}
			
	
			
		}
	    function delcell(cellnum){
	    	$(".cell_"+cellnum).remove();
	    }
	    
	    
	    var cobj;
	    var cdialog;
	    var ismulity=false;
	    //选择器
	    function chooseService(obj,index){
	    	ismulity=false;
	    	
	    	cobj=$(obj);
	    	if(cdialog){
		    	cdialog.close();
	    	}
	    	var ccv = cobj.next().val();
	    	var attname = cobj.attr("attshebei");
	    	cdialog=art.dialog.load("选择系统->序号:"+index,"/category/v_choose_category.do?curent="+ccv+"&attname="+attname);
	    }
	    
	    //选择回调
	    function chooseCallback(name, id,topid){
	    	if(!ismulity){
		    	if(name&&id){
			    	cobj.val(name);//显示名称
			    	cobj.removeClass("btn-danger").addClass("btn-success");
			    	cobj.next().val(id).next().val(topid).next().val(1);//显示值
		    	}
	    	}else{
		    	if(name&&id){
		         	$(".checksystem:checked").each(function(){
			    		var checkedval = $(this).val();
			    		$("#system_"+checkedval).val(name).addClass("btn-success").next().val(id).next().val(topid).next().val(1);
			    		$(this).attr("checked",false);
			    	});
		         	$(".checkedall")[0].checked = false;
		    	}
	    	}
	    	cdialog.close();
	    }
	    
	    $(".checksystem").each(function(){
	    	$(this).click(function(){
	    		var checkedsize = $(".checksystem:checked").size();
	    		if(checkedsize==0){
	    			$("#mulityset").hide();	
	    		}else{
	    			$("#mulityset").show();	
	    		}
	    	});
	    })
	    
	    
	    //批量设置系统
	    function mulityset(){
	    	ismulity=true;
	     	if(cdialog){
		    	cdialog.close();
	    	}
	     	cdialog=art.dialog.load("选择系统->批量设置","/category/v_choose_category.do");
	    }
	    
	    //全选方法
	    $(".checkedall").click(function(){
	    	var setchecked = false;
	    	if($(this).is(":checked")){
	    		setchecked=true;
	    		$("#mulityset").show();	
	    	}else{
	    		$("#mulityset").hide();	
	    	}
	    	
	    	$(".checksystem").each(function(){
				$(this)[0].checked =setchecked;
	    	})
	    });
	    
	    //展示未匹配的
	    function shownocontent(){
	    	art.dialog({
	    		title:"未匹配",
	    	    content: document.getElementById('nocontent')
	    	});
	    }
	    
	</script>
	
	[#else]
		${msg!"没有任何信息"}
	[/#if]
	
</body>
</html>